﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:MiniUML.View.Controls">

    <Style x:Key="GenericCommandButtonStyle" TargetType="{x:Type controls:CommandButton}" BasedOn="{StaticResource {x:Type Button}}">

        <Style.Resources>

            <Style x:Key="ImageStyle" TargetType="{x:Type Image}">
                <Setter Property="Width" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:CommandButton}}, Path=ImageWidth}"/>
                <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:CommandButton}}, Path=ImageHeight}"/>
                <Setter Property="Stretch" Value="Uniform"/>
                <Setter Property="Source" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:CommandButton}}, Path=Image}"/>
            </Style>
            
            <Style x:Key="DisabledImageStyle" TargetType="{x:Type Image}" BasedOn="{StaticResource ImageStyle}">
                <Setter Property="Opacity" Value="0.5"/>
            </Style>

            <DataTemplate x:Key="ImageContentTemplate">

                <Image x:Name="Image" Style="{StaticResource ImageStyle}"/>

                <DataTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Style" Value="{StaticResource DisabledImageStyle}" TargetName="Image"/>
                    </Trigger>
                </DataTemplate.Triggers>
                
            </DataTemplate>
            
            <DataTemplate x:Key="ImageBesideTextContentTemplate">

                <Grid>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Image x:Name="Image" Grid.Column="0" Style="{StaticResource ImageStyle}"/>

                    <TextBlock Grid.Column="1"
                               Margin="4,0,0,0"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>
                </Grid>

                <DataTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Style" Value="{StaticResource DisabledImageStyle}" TargetName="Image"/>
                    </Trigger>
                </DataTemplate.Triggers>

            </DataTemplate>

            <DataTemplate x:Key="ImageAboveTextContentTemplate">

                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Image x:Name="Image" Grid.Row="0" Style="{StaticResource ImageStyle}"/>

                    <TextBlock Grid.Row="1"
                               Margin="0,4,0,0"
                               HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:CommandButton}}, Path=HorizontalContentAlignment}"
                               VerticalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:CommandButton}}, Path=VerticalContentAlignment}"
                               Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/>
                </Grid>

                <DataTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Style" Value="{StaticResource DisabledImageStyle}" TargetName="Image"/>
                    </Trigger>
                </DataTemplate.Triggers>

            </DataTemplate>

        </Style.Resources>

        <Style.Triggers>

            <Trigger Property="ButtonStyle" Value="Text">
                <!-- Use default -->
            </Trigger>
            <Trigger Property="ButtonStyle" Value="Image">
                <Setter Property="ContentTemplate" Value="{StaticResource ImageContentTemplate}"/>
            </Trigger>
            <Trigger Property="ButtonStyle" Value="ImageBesideText">
                <Setter Property="ContentTemplate" Value="{StaticResource ImageBesideTextContentTemplate}"/>
            </Trigger>
            <Trigger Property="ButtonStyle" Value="ImageAboveText">
                <Setter Property="ContentTemplate" Value="{StaticResource ImageAboveTextContentTemplate}"/>
            </Trigger>

        </Style.Triggers>

    </Style>

</ResourceDictionary>